package com.wlb365.financing.common.utils;

import com.wlb365.financing.common.constants.ApiConstants;
import com.wlb365.financing.common.constants.SysConstant;
import com.wlb365.financing.common.exception.AdminException;
import com.wlb365.financing.common.utils.ShiroUtil;
import com.wlb365.financing.modules.sys.entity.Dept;
import com.wlb365.financing.modules.sys.entity.User;
import com.wlb365.financing.modules.trade.response.VoucherQueryRes;
import com.wlb365.sdk.client.SdkClient;
import com.wlb365.sdk.model.BaseRequest;
import com.wlb365.sdk.model.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 概述：
 * </p>
 *
 * @author zwh
 * @version 1.0
 * @copyRight 北京微利宝信息技术有限公司  All right reserved.
 * @date 2021/4/23
 */
@Slf4j
@Component
public class SdkHelper {
    /**sdkclient工具类*/
    private static SdkClient sdkClient;

    @Autowired
    private SdkClient sdkClient1;

    @PostConstruct
    public void init(){
        sdkClient = sdkClient1;
    }

    /**
    * SDK工具类封装.
     *
     * <p>会通过当前登录人，进行机构id传参</p>
    * @param request 请求request
    * @param params 参数
    * @author zwh
    * @return T
    **/
    public static  <T extends BaseResponse> T execute (BaseRequest<T> request, Map<String, Object> params){
        if (params == null){
            params = new HashMap<>();
        }
        //权限

        if(ShiroUtil.isLogin()){
            User user = ShiroUtil.getUser();
            if (!SysConstant.ADMIN.equals(user.getUsername())) {
                if (user.getDept() != null) {
                    if (SysConstant.DEPT_TYPE_0 == user.getDept().getType()){
                        //供应商
                        params.put("supplierId", user.getDept().getId());
                    } else if (SysConstant.DEPT_TYPE_1 == user.getDept().getType()){
                        //核心企业
                        params.put("coreEnterpriseId", user.getDept().getId());
                    } else if (SysConstant.DEPT_TYPE_2 == user.getDept().getType()){
                        //银行
                        params.put("bankId", user.getDept().getId());
                    }
                }
            }
        }

        request.setBizModel(params);
        T res = sdkClient.execute(request);
        res.setData("");
        return res;
    }

    /**
     * SDK工具类封装.
     *
     * <p>自行封装请求参数</p>
     * @param request 请求request
     * @author zwh
     * @return T
     **/
    public static  <T extends BaseResponse> T execute (BaseRequest<T> request){
        T res = sdkClient.execute(request);
        res.setData("");
        return res;
    }

    /**
     * SDK工具类封装，会进行返回报文校验.
     *
     * <p>自行封装请求参数</p>
     * @param request 请求request
     * @author zwh
     * @return T
     **/
    public static  <T extends BaseResponse> T executeAndCheck (BaseRequest<T> request){
        T res = sdkClient.execute(request);
        res.setData("");
        if (!ApiConstants.SUCCESS_CODE.equals(res.getCode())) {
            log.error("调用接口：" +request.getMethod() + "异常。异常原因：" + res.getMsg() + res.getSubMsg());
            throw new AdminException("接口调用异常：" + res.getMsg());
        }
        return res;
    }

}
